namespace soccer {

    interface TouchEvent {
        (e: egret.TouchEvent): void
    }

    export class TableScene extends hjc.Scene {
        public MainGroup: eui.Group
        public TableGroup: eui.Group
        public DebugGroup: eui.Group
        public static Ins: TableScene
        constructor() {
            super()
            TableScene.Ins = this
            game.on(notify.UpdatePos, this.updatePos, this, this)
        }
        private _tapA: egret.Bitmap
        private _tapB: egret.Bitmap
        private _touchPoint: Map<number, helper.PhyObjectType> = new Map<number, helper.PhyObjectType>()
        protected created() {

            let rect = {
                x: 0,
                y: 0,
                width: this.MainGroup.width,
                height: this.MainGroup.height,
                fillColor: Color.RED,
                fillAlpha: 0.2,
                lineAlpha: 0,
            }
            this.MainGroup.addChild(ui.drawRect(rect))

            var s: egret.Sprite = new egret.Sprite()
            this.DebugGroup.addChild(s)
            game.controller(FrontPhyController).resetWorld(s)

            this.TableGroup.touchThrough = false
            this.TableGroup.touchChildren = true
            this.TableGroup.touchEnabled = true

            this.DebugGroup.touchThrough = true
            this.DebugGroup.touchChildren = false
            this.DebugGroup.touchEnabled = false

            this.TableGroup.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.touchMove, this, true)

            {
                this._tapA = new egret.Bitmap()
                this._tapA.texture = RES.getRes("cap_1")
                this.TableGroup.addChild(this._tapA)
                this._tapA.x = 200
                this._tapA.y = 200
                this._tapA.width = 120
                this._tapA.height = 120
                this._tapA.anchorOffsetX = this._tapA.width / 2
                this._tapA.anchorOffsetY = this._tapA.height / 2
                this._tapA.touchEnabled = true
                this._tapA.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.touchBegin(helper.PhyObjectType.TapA), this)
            }
            {
                this._tapB = new egret.Bitmap()
                this._tapB.texture = RES.getRes("cap_2")
                this.TableGroup.addChild(this._tapB)
                this._tapB.x = 400
                this._tapB.y = 400
                this._tapB.width = 120
                this._tapB.height = 120
                this._tapB.anchorOffsetX = this._tapB.width / 2
                this._tapB.anchorOffsetY = this._tapB.height / 2
                this._tapB.touchEnabled = true
                this._tapB.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.touchBegin(helper.PhyObjectType.TapB), this)
            }
        }
        protected displayed() {
        }

        private touchMove(e: egret.TouchEvent) {
            //console.debug(e)
            let tap = this._touchPoint.get(e.touchPointID)
            if (tap) {
                let x = e.localX
                let y = e.localY
                let target = e.target as egret.DisplayObject
                while (target && target != this.TableGroup) {
                    x += target.x - target.anchorOffsetX
                    y += target.y - target.anchorOffsetY
                    target = target.parent
                }
                game.controller(FrontPhyController).updateTapPosition(tap, x, y)
            }
        }
        private touchBegin(tap: helper.PhyObjectType): TouchEvent {
            let f = (e: egret.TouchEvent) => {
                this._touchPoint.set(e.touchPointID, tap)
            }
            return f
        }
        private touchEnd(e: egret.TouchEvent): void {
            this._touchPoint.delete(e.touchPointID)
        }

        private updatePos(ntf: hjc.Notification) {
            //console.debug("TapPosA(%d,%d)", game.proxy(GameProxy).tapAPos.x, game.proxy(GameProxy).tapAPos.y)
            if (game.proxy(GameProxy).tapAPos) {
                this._tapA.x = game.proxy(GameProxy).tapAPos.x * helper.p2m
                this._tapA.y = game.proxy(GameProxy).tapAPos.y * helper.p2m
            }
            if (game.proxy(GameProxy).tapBPos) {
                this._tapB.x = game.proxy(GameProxy).tapBPos.x * helper.p2m
                this._tapB.y = game.proxy(GameProxy).tapBPos.y * helper.p2m
            }
        }
    }
}